package xworker.gluon.glisten.control;

import com.gluonhq.charm.glisten.control.CardCell;
import com.gluonhq.charm.glisten.control.CardPane;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.util.Callback;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.javafx.beans.property.PropertyFactory;
import xworker.javafx.control.ControlActions;
import xworker.javafx.util.JavaFXUtils;
import xworker.lang.util.UtilAction;

import java.util.Collection;

public class CardPaneActions {
    static{
        PropertyFactory.regist(CardPane.class, "onPullToRefreshProperty", o -> {
            CardPane<?> obj = (CardPane<?>) o;
            return obj.onPullToRefreshProperty();
        });
        PropertyFactory.regist(CardPane.class, "placeholderProperty", o -> {
            CardPane<?> obj = (CardPane<?>) o;
            return obj.placeholderProperty();
        });
        PropertyFactory.regist(CardPane.class, "cellFactoryProperty", o -> {
            CardPane<?> obj = (CardPane<?>) o;
            return obj.cellFactoryProperty();
        });
    }

    public static void init(CardPane<Object> node, Thing thing, ActionContext actionContext) {
        ControlActions.init(node, thing, actionContext);

        Callback<CardPane<Object>, CardCell<Object>> cellFactory = JavaFXUtils.getObject(thing, "cellFactory", actionContext);
        if (cellFactory != null) {
            node.setCellFactory(cellFactory);
        }

        EventHandler<ActionEvent> onPullToRefresh = JavaFXUtils.getObject(thing, "onPullToRefresh", actionContext);
        if (onPullToRefresh != null) {
            node.setOnPullToRefresh(onPullToRefresh);
        }

        Node placeholder = JavaFXUtils.getObject(thing, "placeholder", actionContext);
        if (placeholder != null) {
            node.setPlaceholder(placeholder);
        }

        Collection<Object> items = JavaFXUtils.getObject(thing, "items", actionContext);
        if (items != null) {
            node.getItems().addAll(items);
        }
    }

    public static CardPane<Object> create(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        CardPane<Object> node = new CardPane<>();
        init(node, self, actionContext);

        actionContext.peek().put("parent", node);
        for(Thing child : self.getChilds()){
            child.doAction("create", actionContext);
        }

        actionContext.g().put(self.getMetadata().getName(), node);
        return node;
    }

    public static void createCellFactory(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        CardPane<Object> parent = actionContext.getObject("parent");

        parent.setCellFactory(pane -> UtilAction.runChildsAsAction(self, actionContext, "cardPane", pane));
    }

    public static void createCardCellFactory(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        CardPane<Object> parent = actionContext.getObject("parent");

        parent.setCellFactory(pane -> {
            Thing cardCellThing = self.getThing("CardCell");
            if(cardCellThing != null){
                ActionContext ac = new ActionContext();
                ac.put("cardPane", parent);
                ac.put("parentContext", actionContext);
                return cardCellThing.doAction("create", ac);
            }else{
                return null;
            }
        });
    }
}
